package org.lyon_yan.JBCS_framework;

import java.util.List;

import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.layout.Pane;

import org.lyon_yan.JBCS_framework.config.R;
import org.lyon_yan.JBCS_framework.listener.JavaScriptListerner;
import org.lyon_yan.JBCS_framework.view.AppCompatView;

/**
 * 单独的一个APP页面的设计标准
 * 
 * @author Lyon_Yan
 * @since 2015-08-06 09:51:57
 * @version 8.0.1
 *
 */
public abstract class AppCompatActivity extends Pane {
	private AppCompatView appCompatView = null;

	@SuppressWarnings("unused")
	private AppCompatActivity(AppCompatView appCompatView) {
		setAppCompatView(appCompatView);
		init();
	}

	public AppCompatActivity() {
		// TODO Auto-generated constructor stub
		if (appCompatView == null) {
			setAppCompatView(new AppCompatView());
		}
		init();
	}

	/**
	 * 初始化程序
	 */
	private void init() {
		/**
		 * 增加(webUI面板)
		 */
		getChildren().add(onCreate(getAppCompatView()).getWebView());
	}

	/**
	 * 注冊javascript事件监听者<br/>
	 * 可以自己继承JavaScriptListerner的方法
	 * 
	 * @param javaScriptListerner
	 */
	protected abstract void onRegistJavaScriptListener(
			JavaScriptListerner javaScriptListerner);

	/**
	 * 页面创建时所需的方法
	 * 
	 * @param appCompatView
	 * @return
	 */
	protected abstract AppCompatView onCreate(AppCompatView appCompatView);

	@Override
	protected void layoutChildren() {
		List<Node> managed = getManagedChildren();
		double width = getWidth();
		double height = getHeight();
		double top = getInsets().getTop();
		double right = getInsets().getRight();
		double left = getInsets().getLeft();
		double bottom = getInsets().getBottom();
		for (int i = 0; i < managed.size(); i++) {
			Node child = managed.get(i);
			layoutInArea(child, left, top, width - left - right, height - top
					- bottom, 0, Insets.EMPTY, true, true, HPos.CENTER,
					VPos.CENTER);
		}
	}

	public void setAppCompatView(AppCompatView appCompatView) {
		this.appCompatView = appCompatView;
		if (R.page.index != null) {
			appCompatView.setUrl(R.page.index);
		}
		onRegistJavaScriptListener(new JavaScriptListerner(appCompatView
				.getWebView().getEngine()));
	}

	public AppCompatView getAppCompatView() {
		return appCompatView;
	}

}
